﻿using System;
using System.Collections;
using System.Collections.Generic;
//using IBatisNet.DataAccess;
using Wsitech.Model;
using Wsitech.Utility;

namespace Wsitech.BLL
{
    /// <summary>
    /// 功能：     AdminUser业务逻辑实现
    /// 作者：     LI
    /// 日期：     5/16/2012
    /// </summary>
    public class AdminUserBLL : BaseBLL
    {
        private Wsitech.Persistence.Interface.IAdminUserDao dao = null;

        private AdminUserBLL()
        {
            dao = Wsitech.Persistence.Dao.AdminUserSqlMapDao.Instance;
        }

        #region （懒汉）单例模式
        private static AdminUserBLL instance = null;
        private static object syncRoot = new object();
        public static AdminUserBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new AdminUserBLL();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion


        #region	基本功能 不能修改


        /// <summary>
        /// 根据主键得到明细
        /// </summary>
        /// <returns></returns>
        public AdminUser GetDetail(System.String userid)
        {
            try
            {
                return dao.GetDetail(userid);
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public IList<AdminUser> GetList()
        {
            try
            {
                IList<AdminUser> list = dao.GetList();
                return list;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private IList<AdminUser> GetListByParams(object TopCounts, AdminUser adminuser)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                IList<AdminUser> list = dao.GetListByParams(ht);
                return list;
            }
            catch
            {
                throw;
            }
        }



        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="adminuser"></param>
        /// <returns></returns>
        public object Add(AdminUser adminuser)
        {
            try
            {
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Add, GetDetail(adminuser.UserID));
                return dao.Add(adminuser);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="adminuser"></param>
        /// <returns></returns>
        public int Update(AdminUser adminuser)
        {
            try
            {
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Update, GetDetail(adminuser.UserID));
                return dao.Update(adminuser);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public int Delete(System.String userid)
        {
            try
            {
                return dao.Delete(userid);
            }
            catch
            {
                throw;
            }
        }

        #endregion


        #region 扩展功能

        #endregion


        /// <summary>
        /// 修改用户状态
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="state"></param>
        public void ChangeState(string uid, Utility.UtilityEnum.State state)
        {
            AdminUser user = dao.GetDetail(uid);
            if (user != null)
            {
                user.State = (int)state;
                user.UpdateDate = DateTime.Now;

                Update(user);
            }
        }

        /// <summary>
        /// 添加新用户
        /// </summary>
        /// <param name="p"></param>
        /// <param name="p_2"></param>
        public void AddUser(string roleid, string username, string password)
        {
            base.SqlMap.BeginTransaction();
            try
            {

                AdminUser user = new AdminUser();
                user.UserID = Guid.NewGuid().ToString();
                user.UserName = username;
                user.UserPassword = GetEncriptPassword(password);


                user.State = (int)UtilityEnum.State.Enable;
                user.IsDel = (int)UtilityEnum.Del.No;

                user.CreateDate = DateTime.Now;
                user.UpdateDate = DateTime.Now;
                Add(user);


                //添加角色
                UserRoleRelaBLL.Instance.AddRela(roleid, user.UserID);
                base.SqlMap.CommitTransaction();
            }
            catch (Exception)
            {
                base.SqlMap.RollBackTransaction();
                throw;
            }

        }

        /// <summary>
        /// 获取加密后的密码
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public string GetEncriptPassword(string password)
        {
            return base.EncryptMD5(base.EncryptMD5(password) + "LegendBackend").Substring(8, 12);
        }

        /// <summary>
        /// 验证帐户信息
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public AdminUser CheckAccount(string username, string password)
        {
            Hashtable ht = new Hashtable();
            ht.Add("userName", username);
            ht.Add("password", GetEncriptPassword(password));
            IList<AdminUser> list = dao.GetListByParams(ht);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据用户名获取用户信息
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public AdminUser GetDetailByUserName(string username)
        {
            Hashtable ht = new Hashtable();
            ht.Add("userName", username);

            IList<AdminUser> list = dao.GetListByParams(ht);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="p"></param>
        /// <param name="p_2"></param>
        /// <param name="p_3"></param>
        public void UpdateUser(string userid, string roleid, string username, string newpassword)
        {
            AdminUser user = AdminUserBLL.instance.GetDetail(userid);
            if (user != null)
            {
                base.SqlMap.BeginTransaction();
                try
                {
                    user.UserName = username;
                    if (newpassword != "")
                    {
                        user.UserPassword = GetEncriptPassword(newpassword);
                    }
                    user.UpdateDate = DateTime.Now;

                    Update(user);

                    //角色
                    //删除原有角色
                    UserRoleRelaBLL.Instance.DeleteRelaByUserId(userid);

                    //添加新角色
                    UserRoleRelaBLL.Instance.AddRela(roleid, user.UserID);

                    base.SqlMap.CommitTransaction();
                }
                catch (Exception)
                {
                    base.SqlMap.RollBackTransaction();
                    throw;
                }


            }
        }
    }
}
